package com.hzh.util;  
  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;  
  
import org.springframework.web.servlet.HandlerInterceptor;  
import org.springframework.web.servlet.ModelAndView;

import com.hzh.entity.UserEntity;  
/**
 * ClassName: UserLoginInterceptor <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2017年4月28日 下午4:16:20 <br/>
 * 拦截器实现类
 * @author xiaohua
 * @version 
 * @since V1.0.0
 */
public class UserLoginInterceptor implements HandlerInterceptor {  
  
	@Override  
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {  
		
		HttpSession session = request.getSession();  
		// 从session 里面获取用户名的信息  
		UserEntity user = (UserEntity)session.getAttribute("user");
		// 判断如果没有取到用户信息，就跳转到登陆页面，提示用户进行登陆 
		if(user == null){
		    //拦截后跳转到登录页面
			request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);  
			return false;  
		}
		//session中存在该用户则放行
		return true;
	}  
  
	@Override  
	public void postHandle(HttpServletRequest req, HttpServletResponse res, Object arg2, ModelAndView arg3) throws Exception {  
	}  
  
	@Override  
	public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object arg2, Exception arg3) throws Exception {  
	}  
}  